package test3;

class MagicMoney implements Runnable{
    private int money;//线程共享变量
    private boolean ready=false;//累加线程结束标志
    MagicMoney(int money){ this.money=money; }
    //线程体
    synchronized public void run() {//同步方法用 synchronized 修饰
        switch (Thread.currentThread().getName()) { //按名字区分不同线程
            case "累加":
                money+=100;
                ready=true;    //设置累加线程结束标志
                notify();
                break;
            case "翻倍":
                if(!ready) {
                try {
                   wait(); //@@
                } catch (InterruptedException e) {

                }
            }
            money*=2;
            break;
        }
        System.out.println("你手上有"+money+"$");
    }
}
public class Main {
    public static void main(String[] args) {
        MagicMoney m=new MagicMoney(50);//创建接口实现类对象
        Thread s1=new Thread(m,"累加");//创建线程对象
        Thread s2=new Thread(m,"翻倍");
        s2.start();//启动线程
        s1.start();
    }
}